﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using StudentManagementSystem.Data;
using StudentManagementSystem.Model;
namespace StudentManagementSystem.Controllers
{

    [Route("api/[controller]")]
    [ApiController]
    public class SchoolsController : ControllerBase
    {
        private readonly SchoolContext _context;

        public SchoolsController(SchoolContext context)
        {
            _context = context;
        }

        [HttpGet]
        public async Task<IActionResult> GetSchools()
        {
            return Ok(await _context.Schools.ToListAsync());
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetSchool(int id)
        {
            var school = await _context.Schools.FindAsync(id);
            if (school == null)
            {
                return NotFound();
            }
            return Ok(school);
        }

        [HttpPost]
        public async Task<IActionResult> CreateSchool([FromBody] School school)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            _context.Schools.Add(school);
            await _context.SaveChangesAsync();
            await LogAction("Create", $"Created school {school.Name}");

            return CreatedAtAction(nameof(GetSchool), new { id = school.SchoolId }, school);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateSchool(int id, [FromBody] School school)
        {
            if (id != school.SchoolId || !ModelState.IsValid)
            {
                return BadRequest();
            }

            _context.Entry(school).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
                await LogAction("Update", $"Updated school {school.Name}");
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_context.Schools.Any(e => e.SchoolId == id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteSchool(int id)
        {
            var school = await _context.Schools.FindAsync(id);
            if (school == null)
            {
                return NotFound();
            }

            _context.Schools.Remove(school);
            await _context.SaveChangesAsync();
            await LogAction("Delete", $"Deleted school {school.Name}");

            return NoContent();
        }

        private async Task LogAction(string action, string description)
        {
            var log = new Log
            {
                Action = action,
                Description = description,
                TimeStamp = DateTime.UtcNow
            };

            _context.Logs.Add(log);
            await _context.SaveChangesAsync();
        }
    }

}
